package com.qf.elasticsearch.doc.impl;

import com.qf.elasticsearch.doc.DocService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * create by 郑大仙丶
 * 2019/11/21 12:41
 */
@Service
@Slf4j
public class DocServiceImpl implements DocService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public void add(String index, String type, String json) throws IOException {
        //1. 创建索引的request
        IndexRequest request = new IndexRequest(index,type);
        //2. 添加具体文档
        request.source(json, XContentType.JSON);
        //3. 执行添加
        IndexResponse resp = client.index(request, RequestOptions.DEFAULT);
        //4. 输出
        log.info("【添加文档】 resp = {}",resp);
    }

    @Override
    public void add(String index, String type, String json,String id) throws IOException {
        //1. 创建索引的request
        IndexRequest request = new IndexRequest(index,type,id);
        //2. 添加具体文档
        request.source(json, XContentType.JSON);
        //3. 执行添加
        IndexResponse resp = client.index(request, RequestOptions.DEFAULT);
        //4. 输出
        log.info("【添加文档】 resp = {}",resp);
    }

    @Override
    public void update(String index, String type, Map<String, Object> doc, String id) throws IOException {
        //1. 创建修改的request
        UpdateRequest request = new UpdateRequest(index,type,id);
        //2. 设置修改的文档信息
        request.doc(doc);
        //3. 执行修改
        UpdateResponse resp = client.update(request, RequestOptions.DEFAULT);
        //4. 输出
        log.info("【修改文档】  resp = {}",resp);
    }

    @Override
    public void delete(String index, String type, String id) throws IOException {
        //1. 创建删除的request
        DeleteRequest request = new DeleteRequest(index,type,id);
        //2. 执行删除.
        DeleteResponse resp = client.delete(request, RequestOptions.DEFAULT);
        //3. 输出
        log.info("【删除文档】 resp = {}",resp);
    }

    @Override
    public void bulkDelete(String index, String type, String[] ids) throws IOException {
        //1. 创建Bulk
        BulkRequest request = new BulkRequest();
        //2. 添加删除id
        for (String id : ids) {
            request.add(new DeleteRequest(index,type,id));
        }
        //3. 执行删除
        BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
        //4. 输出
        BulkItemResponse[] items = resp.getItems();
        for (BulkItemResponse item : items) {
            log.info("【批量删除】 item = {}",item.getFailure());
        }
    }

    @Override
    public void bulkAddOrUpdate(String index, String type,String[] ids, String[] jsonArray) throws IOException {
        //1. 创建
        BulkRequest request = new BulkRequest();
        //2. 封装
        for (int i = 0; i < ids.length; i++) {
            request.add(new IndexRequest(index,type,ids[i]).source(jsonArray[i],XContentType.JSON));
        }
        //3. 执行
        BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
        //4. 输出
        BulkItemResponse[] items = resp.getItems();
        for (BulkItemResponse item : items) {
            log.info("【批量添加】 item = {}",item.getFailure());
        }


    }

    @Override
    public void refresh(String index) throws IOException {
        //1. 创建
        RefreshRequest request = new RefreshRequest(index);
        //2. 执行
        RefreshResponse resp = client.indices().refresh(request, RequestOptions.DEFAULT);
        //3. 输出
        log.info("【刷新索引】 index = {},resp = {}",index,resp.toString());
    }


}
